package qs.calendar
{
	import com.pbsmedia.colagen.code.SearchUtils;
	import com.pbsmedia.colagen.model.ModelLocator;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	
	import qs.utils.DateRange;
	
	public class CalendarEvent extends EventDispatcher
	{
		public var summary:String;//this is the title;
		public var location:int =0;//this is the roomID
		public var description:String;
		private var _range:DateRange;
		public var id:int = 0;
		public var allDay:int = 0;
		public var requestPackID:int;
		public var repeat:String;
		
		public var properties:Array = [];//this is where our resources live.
		
		//flex
		public var calendar:Calendar;
		
		
		
		public var _color:Number = -1;//eventTypeID
		
		//flex----------
		public var eventTypeName:String = "";
		
		//bj---
		public function CalendarEvent(obj:Object = null){
			range = new DateRange();
			if(obj != null){
				this.summary = obj.summary;
				this.location = obj.location;
				this.description = obj.description;
				this._range.start =  new Date(obj.range.start);
				this._range.end =  new Date(obj.range.end);
				this.id = obj.id;
				this.allDay = obj.allDay;
				this._color = obj.color;
				
				if(obj.properties is Array){
				this.properties	= obj.properties;
				}else{this.properties = parseResourses(obj.properties);}
				
				this.requestPackID = obj.requestPackID;
				this.repeat = obj.repeat;
				}
			}
		
		//--/bj
		
	
		public function get color():int
		{
		
		var colorToRet:int = 0;
		var tempAC:ArrayCollection = SearchUtils.FindWhereIntInAC(_color,ModelLocator.getInstance().eventTypeVOs,["id"]);
		if(tempAC.length > 0){
		colorToRet = tempAC[0].eventColor};
		
		eventTypeName = tempAC[0].name;
		return colorToRet;	
		}
		
		
		public function set color(c:int):void{
			_color = c;
		}
		
		public function set range(value:DateRange):void
		{	
		
			_range = value;
			dispatchEvent(new Event("change"));
		}
		public function get range():DateRange
		{
			return _range;
		}
		
		
			
		public function dump(prefix:String):String
		{
			var result:String;
			var timeFormatter:DateFormatter = new DateFormatter;
			timeFormatter.formatString  = "HH:NN";
			
			//var indent:String = prefix + "\t";
			
			result = timeFormatter.format(start)+" ~ "+timeFormatter.format(end) + "\n";
			result += room + "\n";
			result += repeat + "\n";
			
			for(var i:int = 0;i<properties.length;i++)
			{
				result += properties[i] + "\n";				
			}
			result += "\n";
			return result;			
		}
		
		public function get start():Date { return range.start; }
		public function set start(value:Date):void { range.start = value;}
		
		public function get end():Date { return range.end; }
		public function set end(value:Date):void { range.end = value; }
		
		
		public function parseResourses(res:String):Array{
			var temp_array:Array = new Array();
			if(res != null){
			temp_array = res.split(",");
			}
			//staff names may have commas in so we ned to deal with here
			//if string object in array does not contain : we can presume it is a staff firstname we need to add it to the preceding object and delete it.
			for(var i:int = 0; i<temp_array.length; i++){
				if(temp_array[i].indexOf(":") <0)	{
				temp_array[i-1] = temp_array[i-1]+","+temp_array[i];
				temp_array.splice(i,1)
				}}
				
			return temp_array;
		}
		public function get room():String{
			var stringToRet:String = "No Room";
			var tempAC:ArrayCollection = SearchUtils.FindWhereIntInAC(this.location, ModelLocator.getInstance().roomVOs, ["id"]);
			if(tempAC.length >0){
			stringToRet = tempAC[0].name;
			}
			return stringToRet;
		}
				
					
	}
}


/** 
  * ------------------------------------------------------------------------
  *                       Modification Log
  * Date     	Developer         	Description of Change
  * ----------	-----------------	-----------------------------------------
  * 02/25/2007	Jove Shi			Add set color.
  */